<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/18
 * Time: 0:04
 */

namespace app\admin\model;


use think\Model;
use app\admin\validate\OrderAction as Vali;

class OrderAction extends Model
{
    public function operation($data){
        $data['log_time'] = time();

        // 验证
        $validate = new Vali();
        if(!$validate -> check($data)){
            return echoArr(0, $validate->getError());
        }

        $result = $this -> allowField(true) -> isUpdate(false) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            // 支付时间
            if(($data['pay_status'] == 1 || $data['pay_status'] == 0) && $data['order_status'] == 0 && $data['shipping_status'] == 0){
                $time = time();

                if($data['pay_status'] == 0){
                    $time = '';
                }

                $data['pay_time'] = $time;
            }

            if(!isset($data['invoice_id'])){
                // 修改订单
                model('Order') -> allowField(true) -> isUpdate(true) -> save($data, ['id' => $data['order_id']]);
            }else{
                $data = [
                    'id' => $data['invoice_id'],
                    'status' => $data['invoice_status']
                ];

                // 修改发票状态
                model('Invoice') -> allowField(true) -> isUpdate(true) -> save($data);
            }

            return echoArr(1, '操作成功');
        }
    }
}